package Demo01;

/**
 * @ClassName Test
 * @Description 测试final修饰的常量
 * @Author 李明忠
 * @Date 2025/5/3 22:29
 * @Version 1.0
 */
class Animal{
    //直接赋值
    public final int age = 10;
    //通过实例代码块赋值
    public final int num;
    {
        num = 20;
        System.out.println("实例代码块初始化num");
    }
    //通过构造函数赋值
    public final String name;
    public Animal(){
        this.name = "李华";
        System.out.println("构造函数初始化name");
    }
    //常量
    public static final double PI = 3.14;
    //通过静态代码块赋值
    public static final int MAX_NUM;
    static {
        MAX_NUM = 100;
        System.out.println("静态代码块MAX_NUM被初始化");
    }
}

public class Test {
    public static void main(String[] args) {
        /**
         * final修饰的变量只能自己赋值，编译器不会自动赋值
         * final修饰的变量只能赋值一次
         * final修饰的变量通常会和static和public一起使用
         */
        Animal animal = new Animal();
        System.out.println(animal.age);
        System.out.println(animal.num);
        System.out.println(animal.name);
        System.out.println(Animal.PI);
        System.out.println(Animal.MAX_NUM);
    }
}
